package com.flow.service.impl;

import java.util.List;

import com.flow.dao.IRoleTabDao;
import com.flow.model.PagerModel;
import com.flow.model.RoleTab;
import com.flow.service.IRoleTabService;
import com.flow.tools.StringTools;

/**
 * 角色服务
 * 
 * @author wang
 * @2016-1-6 RoleTabService.java
 * @param <T>
 * 
 */
public class RoleTabService<T> implements IRoleTabService<T> {

	/**
	 * Dao层引用
	 */
	private IRoleTabDao<T> rtDao;

	/**
	 * 添加角色
	 */
	@Override
	public boolean insertRoleTab(RoleTab roleTab) {

		if (null == roleTab)
			return false;

		if (!StringTools.nullOrEmpty(roleTab.getRtName()))
			return false;

		if (null == roleTab.getRtStatus())
			return false;

		try {

			if (rtDao.insertRoleTab(roleTab))
				return true;

		} catch (Exception e) {

			e.printStackTrace();

		}
		return false;
	}

	/**
	 * 更新角色信息
	 */
	@Override
	public boolean updateRoleTab(RoleTab roleTab) {

		if (null == roleTab)
			return false;

		if (!StringTools.nullOrEmpty(roleTab.getRtName()))
			return false;

		if (null == roleTab.getRtStatus())
			return false;

		if (null == roleTab.getRtId() || 0 == roleTab.getRtId())
			return false;

		try {

			if (rtDao.updateRoleTab(roleTab))
				return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;
	}

	/**
	 * 删除角色
	 */
	@Override
	public boolean deleteRoleTab(RoleTab roleTab) {

		if (null == roleTab)
			return false;

		if (null == roleTab.getRtId() || 0 == roleTab.getRtId())
			return false;

		try {

			if (rtDao.deleteRoleTab(roleTab))
				return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;
	}

	/**
	 * 根据ID获取角色
	 */
	@Override
	public RoleTab findRoleTabById(String id) {

		if (!StringTools.nullOrEmpty(id))
			return null;

		try {

			String hql = "from RoleTab where rtId = ?";
			return rtDao.findRoleTabByHql(hql, Integer.valueOf(id));

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;
	}

	/**
	 * 查询角色列表
	 */
	@Override
	public PagerModel findAllRoleTabByPager() {

		PagerModel pager = new PagerModel();

		try {

			String hql = "from RoleTab";
			pager = this.rtDao.findRoleTabPagerModeled(pager, hql);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return pager;
	}

	/**
	 * 验证角色名
	 */
	@Override
	public boolean vlidateName(String name) {

		if (!StringTools.nullOrEmpty(name)) {
			return false;
		}

		try {

			String hql = "from RoleTab where rtName = ?";
			if (null == rtDao.findRoleTabByHql(hql, name)) {
				return true;
			}

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;
	}

	/**
	 * 查询全部
	 */
	@Override
	public List<T> findAllRoleTab() {

		try {

			String hql = "from RoleTab";
			return this.rtDao.findRoleTabAllByHql(hql);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;
	}

	public void setRtDao(IRoleTabDao<T> rtDao) {
		this.rtDao = rtDao;
	}

}
